{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PandasAI Data Platform Guide\n",
    "\n",
    "This notebook demonstrates how to use the PandasAI Data Platform features to save, push, load, and pull dataframes. The platform enables seamless collaboration and version control for your data analysis projects."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's set up PandasAI with your API key. You can get your free API key from [pandabi.ai](https://app.pandabi.ai)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandasai as pai\n",
    "\n",
    "pai.api_key.set(\"your-key-here\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Example Datasets\n",
    "\n",
    "We'll use two example datasets from our data folder:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load heart disease and loans datasets\n",
    "heart_df = pai.read_csv('./data/heart.csv')\n",
    "loans_df = pai.read_csv('./data/loans_payments.csv')\n",
    "\n",
    "# Display first few rows of each dataset\n",
    "print(\"Heart Disease Dataset:\")\n",
    "heart_df.head()\n",
    "\n",
    "print(\"\\nLoans Dataset:\")\n",
    "loans_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Create Dataframes\n",
    "\n",
    "The `create()` method allows you to save your dataframes with metadata and column descriptions. This enriches your data with semantic meaning."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create heart disease dataset with semantic information\n",
    "heart = pai.create(\n",
    "    path=\"my-team/heart\",\n",
    "    name=\"Heart Disease Data\",\n",
    "    df = heart_df,\n",
    "    description=\"Dataset containing heart disease patient information\",\n",
    "    columns=[\n",
    "        {\"name\": \"Age\", \"type\": \"integer\", \"description\": \"Age of the patient in years\"},\n",
    "        {\"name\": \"Sex\", \"type\": \"string\", \"description\": \"Gender of the patient (M/F)\"},\n",
    "        {\"name\": \"ChestPainType\", \"type\": \"string\", \"description\": \"Type of chest pain experienced\"},\n",
    "        {\"name\": \"RestingBP\", \"type\": \"integer\", \"description\": \"Resting blood pressure in mm Hg\"},\n",
    "        {\"name\": \"Cholesterol\", \"type\": \"integer\", \"description\": \"Serum cholesterol in mg/dl\"},\n",
    "        {\"name\": \"FastingBS\", \"type\": \"integer\", \"description\": \"Fasting blood sugar > 120 mg/dl (1: true; 0: false)\"},\n",
    "        {\"name\": \"MaxHR\", \"type\": \"integer\", \"description\": \"Maximum heart rate achieved\"},\n",
    "        {\"name\": \"Oldpeak\", \"type\": \"float\", \"description\": \"ST depression induced by exercise relative to rest\"},\n",
    "        {\"name\": \"HeartDisease\", \"type\": \"integer\", \"description\": \"Output class (1: heart disease; 0: normal)\"}\n",
    "    ]\n",
    ")\n",
    "\n",
    "# Save loans dataset\n",
    "loans = pai.create(\n",
    "    path=\"my-team/loans\",\n",
    "    name=\"Loan Payments Data\",\n",
    "    df = loans_df,\n",
    "    description=\"Dataset containing loan payment information\",\n",
    "    columns=[\n",
    "        {\"name\": \"loan_id\", \"type\": \"integer\", \"description\": \"Unique identifier for each loan\"},\n",
    "        {\"name\": \"amount\", \"type\": \"float\", \"description\": \"Loan amount in dollars\"},\n",
    "        {\"name\": \"term\", \"type\": \"integer\", \"description\": \"Loan term in months\"},\n",
    "        {\"name\": \"interest_rate\", \"type\": \"float\", \"description\": \"Annual interest rate as a percentage\"},\n",
    "        {\"name\": \"payment\", \"type\": \"float\", \"description\": \"Monthly payment amount\"}\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Push to Platform\n",
    "\n",
    "Push your dataframes to make them available to your team. You can optionally specify versions. You do have to add your own unique slug. You find it in settings/organization in app.pandabi.ai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Push datasets to platform\n",
    "heart.push('my-team-slug/heart')\n",
    "loans.push('my-team-slug/loans')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Load from Platform\n",
    "\n",
    "Load existing dataframes that you or your team have pushed to the platform. You can load them once and use acrss different sessions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load datasets from platform\n",
    "loaded_heart = pai.load('my-team-slug/heart')\n",
    "loaded_loans = pai.load('my-team-slug/loans')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Pull Latest Updates\n",
    "\n",
    "If you already have the dataset locally, you can ensure you get the latest version by using the `pull()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pull latest versions\n",
    "latest_heart = pai.pull('my-team-slug/heart')\n",
    "latest_loans = pai.pull('my-team-slug/loans')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chat with the pulled datasets\n",
    "\n",
    "Then simply chat with your team-mates' freshly pulled datasets. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ask questions about both datasets\n",
    "pai.chat('Relastionship between cholesterol and chest type pain', latest_heart)\n",
    "pai.chat('What is the average interest rate for loans?', latest_loans)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
